﻿Public Class SelectDatabase

    Private Sub SelectDatabase_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.SelectFocusControl()
    End Sub


#Region "Properties"

    Public Property ServerName() As String
        Get
            Return Me.txServerName.Text
        End Get
        Set(ByVal value As String)
            Me.txServerName.Text = value
        End Set
    End Property

    Public Property DatabaseName() As String
        Get
            Return Me.txDatabaseName.Text
        End Get
        Set(ByVal value As String)
            Me.txDatabaseName.Text = value
        End Set
    End Property

    Public Property UseWindowsAuthentication() As Boolean
        Get
            Return Me.chkUseWindowsAuthentication.Checked
        End Get
        Set(ByVal value As Boolean)
            Me.chkUseWindowsAuthentication.Checked = value
            Me.SetupUsernamePassword()
        End Set
    End Property


    Public Property Username() As String
        Get
            Return Me.txUsername.Text
        End Get
        Set(ByVal value As String)
            Me.txUsername.Text = value
        End Set
    End Property


    Public Property Password() As String
        Get
            Return Me.txPassword.Text
        End Get
        Set(ByVal value As String)
            Me.txPassword.Text = value
        End Set
    End Property


#End Region




    Private Sub chkUseWindowsAuthentication_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkUseWindowsAuthentication.CheckedChanged
        Me.SetupUsernamePassword()
    End Sub


    Private Sub SetupUsernamePassword()
        Me.txUsername.Enabled = Not Me.chkUseWindowsAuthentication.Checked
        Me.txPassword.Enabled = Not Me.chkUseWindowsAuthentication.Checked
        Me.lbUsername.Enabled = Not Me.chkUseWindowsAuthentication.Checked
        Me.lbPassword.Enabled = Not Me.chkUseWindowsAuthentication.Checked
    End Sub


    Public Function IsUsernameAndPasswordEntered() As Boolean
        If String.IsNullOrEmpty(Me.txUsername.Text) OrElse String.IsNullOrEmpty(Me.txPassword.Text) Then
            Return False
        Else
            Return True
        End If
    End Function

    Public Sub SelectFocusControl()
        If String.IsNullOrEmpty(Me.txServerName.Text) Then
            Me.txServerName.Select()
        ElseIf String.IsNullOrEmpty(Me.txDatabaseName.Text) Then
            Me.txDatabaseName.Select()
        Else
            If Not Me.chkUseWindowsAuthentication.Checked Then
                If String.IsNullOrEmpty(Me.txUsername.Text) Then
                    Me.txUsername.Select()
                ElseIf String.IsNullOrEmpty(Me.txPassword.Text) Then
                    Me.txPassword.Select()
                    Me.ActiveControl = Me.txPassword
                End If

            End If
        End If
    End Sub

End Class